package com.sakura.probe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.common.util.JwtTokenUtil;
import com.sakura.probe.nosql.redis.RedisKey;
import com.sakura.probe.nosql.redis.RedisService;
import com.sakura.probe.pojo.FeedBack;
import com.sakura.probe.pojo.FootPrint;
import com.sakura.probe.pojo.Topic;
import com.sakura.probe.service.FeedBackService;
import com.sakura.probe.service.FootPrintService;
import com.sakura.probe.service.PersonInfoService;
import com.sakura.probe.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(tags = "PersonCenterController",description = "个人中心的相关功能")
@RestController
@Slf4j
@RequestMapping("/person")
public class PersonCenterController {
    @Autowired
    private PersonInfoService personService;
    @Autowired
    private FootPrintService footPrintService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private FeedBackService feedBackService;

    @ApiOperation("初始化个人中心中用户发帖量、点赞量和收藏量")
    @ResponseBody
    @GetMapping("/initPersonTopicInfo")
    public Map<String,Object> initPersonTopicInfo(String token){
        Long userId = Long.valueOf(JwtTokenUtil.getUsernameFromToken(token));
        return personService.getPersonTopicInfo(userId);
    }

    /**
     * 发布、收藏和足迹
     * 发布 : 直接从mysql中读取(通过用户id)
     * 收藏 : 先从redis中读取收藏的话题id,再通过话题id从mysql中读取
     * 足迹 : 直接从mysql中读取话题id,再通过话题id从mysql中读取
     * @param token
     * @param type 1-查看自己发布的帖子、2-查看自己收藏的帖子、3-查看足迹
     * @param pageIndex 页码
     * @param pageSize 一页显示的内容
     * @return
     */
    @ApiOperation("当浏览帖子时添加/更新上该用户的浏览记录")
    @ResponseBody
    @GetMapping("/showBrowseHistory")
    public Map<String,Object> browseHistory(String token,Integer type,Integer pageIndex,Integer pageSize){
        log.info("token="+token+",type="+type+",pageIndex="+pageIndex+",pageSize="+pageSize);
        HashMap<String,Object> modelMap = new HashMap<>();
        Long userId = Long.valueOf(JwtTokenUtil.getUsernameFromToken(token));
        List<Topic> list = new ArrayList<>();
        List<FootPrint> footPrints = new ArrayList<>();
        int count = 0;
        if (type==3){
            //浏览足迹
            footPrints = footPrintService.selectList(userId, pageIndex, pageSize);
            count = footPrintService.count(new QueryWrapper<FootPrint>().eq("user_id", userId));
        }else if (type==2){
            //个人收藏
            //先从redis中读取总的浏览记录
            Set<String> collectSet = redisService.getSet(RedisKey.getPersonCollect() + userId);
            redisService.setSet(RedisKey.getPersonCollect() + userId,collectSet);
            list = (List<Topic>) topicService.listByIds(collectSet);
            count = collectSet.size();
        }else {
            //自己发布的话题
            IPage<Topic> topicIPage = topicService.page(new Page<Topic>().setCurrent(pageIndex).setSize(pageSize).setDesc("last_edit_time"),
                    new QueryWrapper<Topic>().eq("owner_id", userId));
            list = topicIPage.getRecords();
            count = topicService.count(new QueryWrapper<Topic>().eq("owner_id",userId));
        }
        modelMap.put("count",count);
        modelMap.put("success",true);
        modelMap.put("data",type==3?footPrints:list);
        return modelMap;
    }


    @ApiOperation("提交反馈信息")
    @ResponseBody
    @PostMapping("/postFeedBack")
    @RequiresRoles("0")
    public CommonResult postFeedBack(HttpServletRequest request, String suggestion){
        String token = request.getHeader("Token");
        Long userId = Long.valueOf(JwtTokenUtil.getUsernameFromToken(token));
        FeedBack feedBack = new FeedBack(userId,suggestion);
        boolean result = feedBackService.save(feedBack);
        return result?CommonResult.success("提交成功"):CommonResult.failed("提交失败");
    }
}
